home *** CD-ROM | disk | FTP | other *** search
Text File | 1997-04-25 | 15.1 KB | 601 lines | [TEXT/CWIE] |
- // ==================================================
- // CTouchMeApp.cp
- // Copyright (C) 1996-1997 Mizutori Tetsuya
- // July 4 ,1996; August 4, 1996; February 3, 1997; April 23, 1997.
- // ==================================================
- // All documents are pretty-printed in 10-point Geneva font.
-
- #include <TextUtils.h>
- //#include <Balloons.h>
-
- #include <LGrowZone.h>
- #include <LWindow.h>
- #include <LDialogBox.h>
- #include <LString.h>
- //#include <UModalDialogs.h>
- #include <UDrawingState.h>
- #include <UMemoryMgr.h>
- #include <URegistrar.h>
- #include <UDesktop.h>
- #include <UEnvironment.h>
-
- #include <PP_Messages.h>
- #include <PP_Resources.h>
- #include <PPobClasses.h>
-
- #include "touchMeConstants.h"
- #include "touchMeRegistry.h"
- #include "CTouchMeAppleEvents.h"
- #include "CTouchMeApp.h"
- #include "CTouchMePref.h"
- #include "CAppleGuideFile.h"
- #include "CRadioButton.h"
- #include "CDateEditField.h"
- #include "CTouchMeMainWindow.h"
- #include "UFileTools.h"
- #include "UFinderEvents.h"
- #include "UStandardFiles.h"
- #include "UMacOSTools.h"
- #include "UErrorMessage.h"
-
-
- // Patch for displaying a colored alert dialog. Thanks to Rokkaku Fumio.
- #define PATCH_ACTB TRUE
-
- ModalFilterUPP CTouchMeApp::sAlertPatchProc = NULL;
-
-
- // --------------------------------------------------
- // • Main Program
- // --------------------------------------------------
-
- void main( void )
- {
- // Setup the throw and signal actions.
- SetDebugThrow_( debugAction_Alert );
- SetDebugSignal_( debugAction_Alert );
-
- // Initialize the heap. Parameter is number of master handle blocks to allocate.
- InitializeHeap( 3 );
-
- // Initialize the MacOS toolbox.
- UQDGlobals::InitializeToolbox( &qd );
-
- // Install a GrowZone function to catch low memory situations.
- // Parameter is the size of the memory reserve in bytes.
- new LGrowZone( 20000 );
-
- CTouchMeApp theApp;
- theApp.Run();
- }
-
-
- // --------------------------------------------------
- // • CTouchMeApp
- // --------------------------------------------------
- // Constructor
-
- CTouchMeApp::CTouchMeApp()
- {
- // Register functions to create core PowerPlant classes.
- // RegisterAllPPClasses();
- RegisterPainClasses();
-
- mMainWindow = NULL;
-
- // Create a stack array to hold the file specifications.
- mFileArray = (LArray *) new LArray( sizeof( FSSpec ) );
- ThrowIfNil_( mFileArray );
-
- // Create a preferences class object corresponding to "touchMe Prefs" file.
- Str255 theFilename;
- ::GetIndString( theFilename, rSTRx_TouchMe, rSTRx_TouchMe_PrefFile );
- mPref = (CTouchMePref *) new CTouchMePref( theFilename );
- Assert_( mPref != NULL );
-
- // Initialize the preferences reading from preferences file, or by default value.
- mPref->LoadPrefsData();
-
- // Prepare Apple Guide file.
- mAppleGuideFile = (CAppleGuideFile *) new CAppleGuideFile( kThisApplicationCreatorType );
- Assert_( mAppleGuideFile != NULL );
-
- // Most of these flags are reset every time at idle time operation 'UseIdleTime()'.
- // Setup my program process status.
- mOpenApplication = false;
- mOpenDocument = false;
- mKeyModifier = false;
- mUpdatePref = false;
-
- // Reset the document counter.
- mCount = 0;
-
- // Check if cmd-period key is pressed.
- mIsCommandPeriod = false;
-
- #ifdef PATCH_ACTB
- sAlertPatchProc = NewModalFilterProc( MyAlertPatch );
- #endif // PATCH_ACTB
- }
-
-
- // --------------------------------------------------
- // • ~CTouchMeApp
- // --------------------------------------------------
- // Destructor
-
- CTouchMeApp::~CTouchMeApp()
- {
- if ( mFileArray != NULL ) delete mFileArray;
-
- if ( mPref != NULL ) delete mPref;
-
- if ( mAppleGuideFile != NULL ) delete mAppleGuideFile;
-
- #ifdef PATCH_ACTB
-
- if ( sAlertPatchProc != NULL ) {
- DisposeRoutineDescriptor( sAlertPatchProc );
- }
- sAlertPatchProc = NULL;
-
- #endif // PATCH_ACTB
- }
-
-
- // --------------------------------------------------
- // • StartUp
- // --------------------------------------------------
-
- void
- CTouchMeApp::StartUp()
- {
- // Setup my program process status.
- mOpenApplication = true;
-
- // Let's create a main window.
- if ( mMainWindow == NULL ) {
- mMainWindow = MakeMainWindow();
- } else {
- ((CTouchMeMainWindow *) mMainWindow)->Show();
- }
- }
-
-
- // --------------------------------------------------
- // • OpenDocument
- // --------------------------------------------------
-
- void
- CTouchMeApp::OpenDocument(
- FSSpec * inMacFSSpec )
- {
- // Setup my program process status.
- mOpenDocument = true;
-
- // If cmd-period key was pressed during operation, then terminate the operation.
- if ( mIsCommandPeriod || UMacOSTools::IsCommandPeriod() ) {
- mIsCommandPeriod = true;
- return;
- }
-
- // All documents are to be collected into the file array stack.
- if ( mFileArray == NULL ) return;
-
- // Read the search level from the settings of main window if necessary.
- if ( mUpdatePref && mMainWindow != NULL ) {
- mPref->GetPrefsFromWindow( mMainWindow );
- mUpdatePref = false;
- }
-
- // Put it on the file array stack.
- mFileArray->InsertItemsAt( 1, LArray::index_Last, (void *) inMacFSSpec );
- mCount += 1;
- }
-
-
- // --------------------------------------------------
- // • ChooseDocument
- // --------------------------------------------------
-
- void
- CTouchMeApp::ChooseDocument()
- {
- // Deactivate the desktop.
- UDesktop::Deactivate();
-
- // Browse for a document.
- SFTypeList theTypeList; // = {'TEXT', 'JPEG'};
- StandardFileReply theReply;
- // ::StandardGetFile( NULL, -1, theTypeList, &theReply );
- UStandardFiles::StandardGetFileOrDirectory( NULL, -1, theTypeList, &theReply );
- // UStandardFiles::StandardGetFileOrDirectory( NULL, 2, theTypeList, &theReply );
-
- // Activate the desktop.
- UDesktop::Activate();
-
- #ifdef COMMENT
- if ( theReply.sfGood ) {
- SendAEOpenDoc( theReply.sfFile );
- if ( theReply.sfIsFolder || theReply.sfIsVolume ) {
- // If the choosen FSSpec is a directory, then servey all files in it.
- SurveyFiles( searchLevel_one, theReply.sfFile );
- } else {
- // If the choosen FSSpec is a plain file, then push it on the file array stack.
- SendAEOpenDoc( theReply.sfFile );
- }
- }
- #endif // COMMENT
-
- // Send an apple event to open the file.
- if ( theReply.sfGood ) SendAEOpenDoc( theReply.sfFile );
-
- }
-
-
- // --------------------------------------------------
- // • ObeyCommand
- // --------------------------------------------------
- // Respond to commands
-
- Boolean
- CTouchMeApp::ObeyCommand(
- CommandT inCommand,
- void * ioParam)
- {
- Boolean cmdHandled = true;
-
- switch ( inCommand ) {
- case msg_Main_ButtonOK:
- case msg_Main_ButtonCencel:
- {
- delete mPref;
- if ( mMainWindow != NULL ) delete (CTouchMeMainWindow *) mMainWindow;
-
- SendAEQuit();
- }
- break;
-
- case cmd_Save:
- case msg_SavePrefs:
- {
- // Save all the settings to the preferences file.
- if ( mMainWindow != NULL ) mPref->GetPrefsFromWindow( mMainWindow );
- mPref->SavePrefsData();
- }
- break;
-
- case msg_SavePrefsWFrame:
- {
- // Save the window position only, but not all the other settings.
- if ( mMainWindow != NULL ) mPref->GetPrefsFromWindow( mMainWindow );
-
- Rect theRect;
- mPref->GetWindowRect( theRect );
- // Revert the settings to read from preferences file, and set the window position.
- mPref->LoadPrefsData();
- mPref->SetWindowRect( theRect );
- mPref->SavePrefsData();
- }
- break;
-
- case cmd_Close:
- {
- // Do nothing here.
- }
- break;
-
- case msg_Help:
- {
- // Open Apple Guide file.
- mAppleGuideFile->Open();
- }
- break;
-
- case msg_Touch:
- {
- // Touch files. Process:SendAEOpenDoc() -> HandleDocuments().
- FSSpec * theFSSpec = (FSSpec *) ioParam;
- SendAEOpenDoc( *theFSSpec );
- }
- break;
-
- default:
- {
- cmdHandled = LDocApplication::ObeyCommand( inCommand, ioParam );
- }
- break;
- }
-
- return cmdHandled;
- }
-
-
- // --------------------------------------------------
- // • FindCommandStatus
- // --------------------------------------------------
-
- void
- CTouchMeApp::FindCommandStatus(
- CommandT inCommand,
- Boolean & outEnabled,
- Boolean & outUsesMark,
- Char16 & outMark,
- Str255 outName )
- {
- switch ( inCommand ) {
- case cmd_About:
- case cmd_Open:
- case cmd_Close:
- case cmd_Quit:
- outEnabled = true;
- break;
- case cmd_New:
- case cmd_Save:
- case cmd_PageSetup:
- outEnabled = false;
- break;
- default:
- LDocApplication::FindCommandStatus( inCommand,
- outEnabled, outUsesMark, outMark, outName );
- break;
- }
- }
-
-
- // --------------------------------------------------
- // • UseIdleTime
- // --------------------------------------------------
-
- void
- CTouchMeApp::UseIdleTime(
- const EventRecord & inMacEvent )
- {
- LDocApplication::UseIdleTime( inMacEvent );
-
- if ( inMacEvent.what == nullEvent ) {
- // Check the modifier key when opening the documents.
- // mKeyModifier = ( (inMacEvent.modifiers & ( optionKey | controlKey) ) != 0 );
- mKeyModifier = UMacOSTools::IsModifierKeyPressed( inMacEvent, optionKey | controlKey );
-
- // If the application was invoked by drag and drop, the main window is not displayed.
- if ( mMainWindow != NULL ) {
- ((CTouchMeMainWindow *) mMainWindow)->Indicator( mKeyModifier );
- }
- }
-
- // Reset counter for the incoming document files.
- mCount = 0;
-
- // Each time before a series of operations, prefs data must be updated.
- mUpdatePref = true;
-
- // Do with the files stacked in the file array.
- HandleDocuments();
-
- // Here, flag of cmd-period pressing is cleared every once idle time begins.
- mIsCommandPeriod = false;
-
- // To quit immediately after the drag&drop operation finished.
- // Do not quit when you have opened the application by a double-click.
- if ( ! mOpenApplication && mOpenDocument ) SendAEQuit();
- }
-
-
- // --------------------------------------------------
- // • ShowAboutBox
- // --------------------------------------------------
-
- void
- CTouchMeApp::ShowAboutBox()
- {
- #ifdef PATCH_ACTB
-
- UDesktop::Deactivate();
- ::Alert( ALRT_About, sAlertPatchProc );
- UDesktop::Activate();
-
- #else // PATCH_ACTB
-
- LDocApplication::ShowAboutBox();
-
- #endif // PATCH_ACTB
- }
-
-
- #ifdef PATCH_ACTB
- // --------------------------------------------------
- // • MyAlertPatch
- // --------------------------------------------------
- // Patch for the colored alert dialog. Thanks to Rokkaku Fumio.
-
- #include <UKeyFilters.h>
-
- pascal Boolean
- CTouchMeApp::MyAlertPatch(
- DialogRef theDialog,
- EventRecord * theEvent,
- short * itemHit )
- {
- Boolean eventOccurred = false;
-
- switch ( theEvent->what ) {
- case keyDown:
- {
- short charCode = (theEvent->message & charCodeMask);
- // if ( (charCode ==kEnterKey ) || (charCode ==kReturnKey) ) {
- if ( UKeyFilters::IsActionKey( charCode ) ) {
- ControlRef aButton;
- Rect aRect;
- short aType;
- long finalTick;
-
- ::GetDialogItem( theDialog, ok, &aType, (Handle *)&aButton, &aRect );
- ::HiliteControl( aButton, kControlButtonPart );
- ::Delay( 8, &finalTick );
- ::HiliteControl( aButton, kControlNoPart );
- *itemHit = kStdOkItemIndex;
- eventOccurred = true;
- }
- }
- break;
-
- case updateEvt:
- {
- WindowRef theWindow = ::GetDialogWindow( theDialog );
- if ( (WindowRef)theEvent->message == theWindow ) {
- ::SelectWindow( theWindow );
- }
- }
- break;
- }
- return eventOccurred;
- }
- #endif // PATCH_ACTB
-
-
- // ==================================================
- // Application-spicific functions
- // ==================================================
-
- // --------------------------------------------------
- // • MakeMainWindow
- // --------------------------------------------------
-
- LWindow *
- CTouchMeApp::MakeMainWindow()
- {
- LWindow * theWindow;
-
- // Create the window.
- theWindow = CTouchMeMainWindow::CreateWindow( rPPob_TouchMeMainWindow, this );
- Assert_( theWindow != NULL );
-
- // Set the settings of the window.
- mPref->SetPrefsToWindow( theWindow );
-
- // Show the window.
- theWindow->Show();
-
- return theWindow;
- }
-
-
- // --------------------------------------------------
- // • HandleDocuments
- // --------------------------------------------------
- // This procedure is the major part of touchMe program.
- // If option-key is pressed, it works as a 'fetch' command.
- // If not, it works as a 'touch' command regularly.
-
- void
- CTouchMeApp::HandleDocuments( void )
- {
- if ( mFileArray == NULL ) return;
-
- // Let's retrieve all the files stored in the file array.
- unsigned long theItemCount = mFileArray->GetCount();
- if ( theItemCount == 0 ) return;
-
- // Load settings from main window if necessary.
- if ( mUpdatePref && mMainWindow != NULL ) {
- mPref->GetPrefsFromWindow( mMainWindow );
- mUpdatePref = false;
- }
-
- ETouchFlag flagCr = mPref->GetFlagNumb( touchType_CreationDate );
- ETouchFlag flagMd = mPref->GetFlagNumb( touchType_ModificationDate );
- Boolean toUpdateCr = mPref->GetEnabled( touchType_CreationDate );
- Boolean toUpdateMd = mPref->GetEnabled( touchType_ModificationDate );
- Boolean toQuit = false;
-
- unsigned long theCrDateTime = 0, theMdDateTime = 0;
- unsigned long theDateTimeSecs;
- ::GetDateTime( &theDateTimeSecs ); // First, set the current date time.
-
- // Try to handle each file.
- for ( ArrayIndexT index = LArray::index_First; (index <= theItemCount) && ! toQuit; index++ ) {
-
- // Pressing cmd-period terminates the execution.
- if ( mIsCommandPeriod || UMacOSTools::IsCommandPeriod() ) {
- mIsCommandPeriod = true;
- break;
- }
-
- FSSpec theFSSpec;
- mFileArray->FetchItemAt( index, &theFSSpec );
-
- // Check the modifier key whether or not the option-key is pressed.
- // If option-key is pressed, it works as a 'fetch' command and exits immediately.
- if ( mKeyModifier ) {
- if ( mMainWindow != NULL )
- ((CTouchMeMainWindow *)mMainWindow)->BroadcastMessage( msg_Main_DroppedFile, (void *)&theFSSpec );
- break;
- }
-
- // Do special case if the item is the first one.
- if ( index == LArray::index_First ) {
-
- // Fetch the date time stamp from the first dropped file.
- UFileTools::GetFSSpecDateTime( theFSSpec, theCrDateTime, theMdDateTime );
-
- // Creation date:
- // Setup the date time stamp according to the flag of settings.
- switch ( flagCr ) {
- case touchFlag_Current:
- theCrDateTime = theDateTimeSecs;
- break;
- case touchFlag_Direct:
- theCrDateTime = mPref->GetDateTime( touchType_CreationDate );
- break;
- case touchFlag_First:
- break;
- case touchFlag_Second:
- mPref->SetDateTime( touchType_CreationDate, theCrDateTime );
- if ( mMainWindow != NULL ) mPref->SetPrefsToWindow( mMainWindow );
- break;
- }
-
- // Modification date:
- // Setup the date time stamp according to the flag of settings.
- switch ( flagMd ) {
- case touchFlag_Current:
- theMdDateTime = theDateTimeSecs;
- break;
- case touchFlag_Direct:
- theMdDateTime = mPref->GetDateTime( touchType_ModificationDate );
- break;
- case touchFlag_First:
- break;
- case touchFlag_Second:
- mPref->SetDateTime( touchType_ModificationDate, theMdDateTime );
- if ( mMainWindow != NULL ) mPref->SetPrefsToWindow( mMainWindow );
- break;
- }
-
- }
-
- // If the{Cr,Md}DateTime has a zero value, then its date time of FSSpec wll not be changed.
- if ( ! toUpdateCr ) theCrDateTime = 0;
- if ( ! toUpdateMd ) theMdDateTime = 0;
- UFileTools::SetFSSpecDateTime( theFSSpec, theCrDateTime, theMdDateTime );
-
- }
-
- // Get Info if shift key is pressed.
- if ( UMacOSTools::IsModifierKeyPressed(shiftKey) ) {
- for ( ArrayIndexT index = LArray::index_First; index <= theItemCount; index++ ) {
- FSSpec theFSSpec;
- mFileArray->FetchItemAt( index, &theFSSpec );
- UFinderEvents::GetInfoSelection( theFSSpec );
- }
- }
-
- // Discard all the files stored in the file array.
- mFileArray->RemoveItemsAt( theItemCount, LArray::index_First );
-
- }
-
-
- // end of program
-